﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DMC.Database;
namespace prjLichSuXemBienBanXetDuyet
{
    public partial class XemLichSuHoSoBienDong : UserControl
    {
        private DataTable dtfull = new DataTable(); 
        clsDatabase clsData = new clsDatabase();
        public string MaDVHC;
        public string MaHoSoCapGCNcu;
        public string MaHoSoCapGCN;
        public string curMaDVHC;
        public string curMaHoSoCapGCN;
        public string curID;
        public string DonViHanhChinh        
        {
            get { return MaDVHC; }
            set { MaDVHC = value; }
        }
        public string HoSoCapGCNcu
        {
            get { return MaHoSoCapGCNcu; }
            set { MaHoSoCapGCNcu = value; }
        }
        public string HoSoCapGCN
        {
            get { return MaHoSoCapGCN; }
            set { MaHoSoCapGCN = value; }
        }
        public XemLichSuHoSoBienDong()
        {
            InitializeComponent();
        }

        public string strConnection;
        public string Connection 
        {
            get { return strConnection; }
            set { strConnection = value; }
        }
        public void HienThiData()
        {
            clsLichSuXemBienBanXetDuyet cls = new clsLichSuXemBienBanXetDuyet();
            cls.Connection = strConnection;
            DataTable dt = new DataTable();
            cls.getLichSuBienBanXetDuyet(ref dt);
           // BindingSource bd = new BindingSource();
           //pngNavigator1.BindingSource = bd;
           // ctrlGridView1.DataSource = bd;
            ctrlGridView1.DataSource = dt;
            dtfull = dt.Clone();
            dtfull = dt;
            //cboMaHoSoCapGCN.DataSource = dtfull;
        }
        public void DinhDangGirdview()
        {
            ctrlGridView1.Columns["NgayXem"].HeaderText = "Ngày xem";
            ctrlGridView1.Columns["ID"].HeaderText = "ID";
            ctrlGridView1.Columns["MaDVHC"].HeaderText = "Mã đơn vị hành chính";
            ctrlGridView1.Columns["MaHoSoCapGCN"].HeaderText = "Mã hồ sơ cấp giấy chứng nhận";
            ctrlGridView1.Columns["Ten"].HeaderText = "Tên đơn vị hành chính";
            
            ctrlGridView1.Columns["MaDVHC"].Width = 100;
            ctrlGridView1.Columns["Ten"].Width = 200;
            ctrlGridView1.Columns["MaHoSoCapGCN"].Width = 150;
            ctrlGridView1.Columns["NgayXem"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;           
        }
        private void dtpTuNgay_VisibleChanged(object sender, EventArgs e)
        {            
        }
        //private void chkMaHoSoCapGCN_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (chkMaHoSoCapGCN.Checked == true)
        //        cboMaHoSoCapGCN.Enabled = true;
        //    else cboMaHoSoCapGCN.Enabled = false;
        //    if (chkMaHoSoCapGCN.Checked == true)
        //    {
        //        if (dtpTuNgay.Checked == false)
        //        {
        //            DataTable dttemp = new DataTable();
        //            try
        //            {
        //                if (dtfull.Select("MaHoSoCapGCN =" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "") == null)
        //                {
        //                    ctrlGridView1.DataSource = null;
        //                }
        //                else
        //                {
        //                    DataRow[] drs = dtfull.Select("MaHoSoCapGCN =" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "");
        //                    dttemp = dtfull.Clone();
        //                    foreach (DataRow d in drs)
        //                    {
        //                        dttemp.ImportRow(d);
        //                    }
        //                    ctrlGridView1.DataSource = dttemp;
        //                    DinhDangGirdview();
        //                }
        //            }
        //            catch (Exception ex)
        //            {
        //                // MessageBox.Show(ex.ToString());
        //            }
        //        }
        //        else
        //        {
        //            DataTable dttemp = new DataTable();
        //            try
        //            {
        //                if (dtfull.Select("NgayXem >= #" + dtpTuNgay.Value + "# and NgayXem <= #" + dtpDenNgay.Value + "# and MaHoSoCapGCN=" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "") == null)
        //                {
        //                    ctrlGridView1.DataSource = null;
        //                }
        //                else
        //                {
        //                    DataRow[] drs = dtfull.Select("NgayXem >= #" + dtpTuNgay.Value + "# and NgayXem <= #" + dtpDenNgay.Value + "# and MaHoSoCapGCN=" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "");
        //                    dttemp = dtfull.Clone();
        //                    foreach (DataRow d in drs)
        //                    {
        //                        dttemp.ImportRow(d);
        //                    }
        //                    ctrlGridView1.DataSource = dttemp;
        //                    DinhDangGirdview();
        //                }
        //            }
        //            catch (Exception ex)
        //            {
        //                // MessageBox.Show(ex.ToString());
        //            }
        //        }
        //    }
        //    else
        //    {
        //        if (dtpTuNgay.Checked == false)
        //        {
        //            ctrlGridView1.DataSource = dtfull;
        //            DinhDangGirdview();
        //        }
        //        else
        //        {
        //            DataTable dttemp = new DataTable();
        //            DataRow[] drs = dtfull.Select("NgayXem >= #" + dtpTuNgay.Value + "# and NgayXem <= #" + dtpDenNgay.Value + "#");
        //            dttemp = dtfull.Clone();
        //            foreach (DataRow d in drs)
        //            {
        //                dttemp.ImportRow(d);
        //            }
        //            ctrlGridView1.DataSource = dttemp;
        //            DinhDangGirdview();
        //        }
        //    }
        //}
        private void txtMaHoSoCapGCN_TextChanged(object sender, EventArgs e)
        {                       
        }
        private void dtpTuNgay_ValueChanged(object sender, EventArgs e)
        {
            if (dtpTuNgay.Checked == true) dtpDenNgay.Checked = true;
            else dtpDenNgay.Checked = false;
            if (dtpTuNgay.Checked == true)
            {
                if (txtMaHoSoCapGCN.Text != "")
                {
                    DataTable dttemp = new DataTable();
                    try
                    {
                        DataRow[] drs = dtfull.Select("NgayXem >= #" + dtpTuNgay.Value + "# and NgayXem <= #" + dtpDenNgay.Value + "# and MaHoSoCapGCN=" + txtMaHoSoCapGCN.Text);
                        dttemp = dtfull.Clone();
                        foreach (DataRow d in drs)
                        {
                            dttemp.ImportRow(d);
                        }
                        ctrlGridView1.DataSource = dttemp;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
            else
            {
                DataTable dttemp = new DataTable();
                try
                {
                    DataRow[] drs = dtfull.Select("MaHoSoCapGCN="+txtMaHoSoCapGCN.Text);
                    dttemp = dtfull.Clone();
                    foreach (DataRow d in drs)
                    {
                        dttemp.ImportRow(d);
                    }
                    ctrlGridView1.DataSource = dttemp;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            //if (dtpTuNgay.Checked == true)
            //{
            //    if (chkMaHoSoCapGCN.Checked == false)
            //    {
            //        DataTable dttemp = new DataTable();
            //        try
            //        {
            //            DataRow[] drs = dtfull.Select("NgayXem >= #" + dtpTuNgay.Value + "# and NgayXem <= #" + dtpDenNgay.Value + "#");
            //            dttemp = dtfull.Clone();
            //            foreach (DataRow d in drs)
            //            {
            //                dttemp.ImportRow(d);
            //            }
            //            ctrlGridView1.DataSource = dttemp;
            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.ToString());
            //        }
            //    }
            //    else
            //    {
            //        DataTable dttemp = new DataTable();
            //        try
            //        {
            //            DataRow[] drs = dtfull.Select("NgayXem >= #" + dtpTuNgay.Value + "# and NgayXem <= #" + dtpDenNgay.Value + "# and MaHoSoCapGCN=" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "");
            //            dttemp = dtfull.Clone();
            //            foreach (DataRow d in drs)
            //            {
            //                dttemp.ImportRow(d);
            //            }
            //            ctrlGridView1.DataSource = dttemp;
            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.ToString());
            //        }
            //    }
            //}
            //else
            //{
            //    DataTable dttemp = new DataTable();
            //    if (chkMaHoSoCapGCN.Checked == false)
            //    {
            //        ctrlGridView1.DataSource = dtfull;
            //        DinhDangGirdview();
            //    }
            //    else
            //    {
            //        DataRow[] drs = dtfull.Select("MaHoSoCapGCN =" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "");
            //        dttemp = dtfull.Clone();
            //        foreach (DataRow d in drs)
            //        {
            //            dttemp.ImportRow(d);
            //        }
            //        ctrlGridView1.DataSource = dttemp;
            //        DinhDangGirdview();
            //    }

            //}
        }
        private void dtpDenNgay_ValueChanged(object sender, EventArgs e)
        {
            if (dtpDenNgay.Checked == true) dtpTuNgay.Checked = true;
            else dtpTuNgay.Checked = false;
            if (dtpTuNgay.Checked == true)
            {
                if (txtMaHoSoCapGCN.Text != "")
                {
                    DataTable dttemp = new DataTable();
                    try
                    {
                        DataRow[] drs = dtfull.Select("NgayXem >= #" + dtpTuNgay.Value + "# and NgayXem <= #" + dtpDenNgay.Value + "# and MaHoSoCapGCN=" + txtMaHoSoCapGCN.Text);
                        dttemp = dtfull.Clone();
                        foreach (DataRow d in drs)
                        {
                            dttemp.ImportRow(d);
                        }
                        ctrlGridView1.DataSource = dttemp;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
            else
            {
                DataTable dttemp = new DataTable();
                try
                {
                    DataRow[] drs = dtfull.Select("MaHoSoCapGCN=" + txtMaHoSoCapGCN.Text);
                    dttemp = dtfull.Clone();
                    foreach (DataRow d in drs)
                    {
                        dttemp.ImportRow(d);
                    }
                    ctrlGridView1.DataSource = dttemp;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            //if (dtpDenNgay.Checked == true)
            //{
            //    if (chkMaHoSoCapGCN.Checked == false)
            //    {
            //        DataTable dttemp = new DataTable();
            //        try
            //        {
            //            DataRow[] drs = dtfull.Select("NgayXem >= #" + dtpTuNgay.Value + "# and NgayXem <= #" + dtpDenNgay.Value + "#");
            //            dttemp = dtfull.Clone();
            //            foreach (DataRow d in drs)
            //            {
            //                dttemp.ImportRow(d);
            //            }
            //            ctrlGridView1.DataSource = dttemp;
            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.ToString());
            //        }
            //    }
            //    else
            //    {
            //        DataTable dttemp = new DataTable();
            //        try
            //        {
            //            DataRow[] drs = dtfull.Select("NgayXem >= #" + dtpTuNgay.Value + "# and NgayXem <= #" + dtpDenNgay.Value + "# and MaHoSoCapGCN=" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "");
            //            dttemp = dtfull.Clone();
            //            foreach (DataRow d in drs)
            //            {
            //                dttemp.ImportRow(d);
            //            }
            //            ctrlGridView1.DataSource = dttemp;
            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.ToString());
            //        }
            //    }
            //}
            //else
            //{
            //    DataTable dttemp = new DataTable();
            //    if (chkMaHoSoCapGCN.Checked == false)
            //    {
            //        ctrlGridView1.DataSource = dtfull;
            //        DinhDangGirdview();
            //    }
            //    else
            //    {
            //        DataRow[] drs = dtfull.Select("MaHoSoCapGCN =" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "");
            //        dttemp = dtfull.Clone();
            //        foreach (DataRow d in drs)
            //        {
            //            dttemp.ImportRow(d);
            //        }
            //        ctrlGridView1.DataSource = dttemp;
            //        DinhDangGirdview();
            //    }

            //}
        }      
        private void cboMaHoSoCapGCN_SelectedValueChanged(object sender, EventArgs e)
        {
            //if (chkMaHoSoCapGCN.Checked == true)
            //{
            //    if (dtpTuNgay.Checked == false)
            //    {
            //        DataTable dttemp = new DataTable();
            //        try
            //        {
            //            if (dtfull.Select("MaHoSoCapGCN =" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "") == null)
            //            {
            //                ctrlGridView1.DataSource = null;
            //            }
            //            else
            //            {
            //                DataRow[] drs = dtfull.Select("MaHoSoCapGCN =" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "");
            //                dttemp = dtfull.Clone();
            //                foreach (DataRow d in drs)
            //                {
            //                    dttemp.ImportRow(d);
            //                }
            //                ctrlGridView1.DataSource = dttemp;
            //                DinhDangGirdview();
            //            }
            //        }
            //        catch (Exception ex)
            //        {
            //            // MessageBox.Show(ex.ToString());
            //        }
            //    }
            //    else
            //    {
            //        DataTable dttemp = new DataTable();
            //        try
            //        {
            //            if (dtfull.Select("NgayXem >= #" + dtpTuNgay.Value + "# and NgayXem <= #" + dtpDenNgay.Value + "# and MaHoSoCapGCN=" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "") == null)
            //            {
            //                ctrlGridView1.DataSource = null;
            //            }
            //            else
            //            {
            //                DataRow[] drs = dtfull.Select("NgayXem >= #" + dtpTuNgay.Value + "# and NgayXem <= #" + dtpDenNgay.Value + "# and MaHoSoCapGCN=" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "");
            //                dttemp = dtfull.Clone();
            //                foreach (DataRow d in drs)
            //                {
            //                    dttemp.ImportRow(d);
            //                }
            //                ctrlGridView1.DataSource = dttemp;
            //                DinhDangGirdview();
            //            }
            //        }
            //        catch (Exception ex)
            //        {
            //            // MessageBox.Show(ex.ToString());
            //        }
            //    }
            //}
            //else
            //{
            //    if (dtpTuNgay.Checked == false)
            //    {
            //        ctrlGridView1.DataSource = dtfull;
            //        DinhDangGirdview();
            //    }
            //    else
            //    {
            //        DataTable dttemp = new DataTable();
            //        DataRow[] drs = dtfull.Select("NgayXem >= #" + dtpTuNgay.Value + "# and NgayXem <= #" + dtpDenNgay.Value + "#");
            //        dttemp = dtfull.Clone();
            //        foreach (DataRow d in drs)
            //        {
            //            dttemp.ImportRow(d);
            //        }
            //        ctrlGridView1.DataSource = dttemp;
            //        DinhDangGirdview();
            //    }
            //}
        }
        private void cboMaHoSoCapGCN_KeyUp(object sender, KeyEventArgs e)
        {
            //if (chkMaHoSoCapGCN.Checked == true)
            //{
            //    try
            //    {
            //        DataRow[] drs = dtfull.Select("MaHoSoCapGCN =" + int.Parse(cboMaHoSoCapGCN.SelectedValue.ToString()) + "");
            //        if (drs.Length == 0)
            //            ctrlGridView1.DataSource = null;
            //    }
            //    catch { ctrlGridView1.DataSource = null; }
            //}
        }
        private void ctrlGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            curMaDVHC = ctrlGridView1.Rows[e.RowIndex].Cells["MaDVHC"].Value.ToString();
            curMaHoSoCapGCN = ctrlGridView1.Rows[e.RowIndex].Cells["MaHoSoCapGCN"].Value.ToString();
            curID = ctrlGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString();
            //MessageBox.Show(curMaDVHC + " " + curMaHoSoCapGCN);
        }
        private void ctrlGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {            
            Form frm = new frmLichSuXemBienBanXetDuyetChiTiet(curID,curMaDVHC, curMaHoSoCapGCN,strConnection,MaHoSoCapGCNcu);                        
            frm.ShowDialog();
        }
        private void XemLichSuHoSoBienDong_Load(object sender, EventArgs e)
        {
           
        }
        public void LoadGird()
        {
            DataTable dttmp = new DataTable();
            int rowdt = dtfull.Rows.Count;
            bool kt = false;
            for (int i = 0; i < rowdt ; i++)
            {
                if (MaHoSoCapGCNcu == dtfull.Rows[i]["MaHoSoCapGCN"].ToString())
                {
                    txtMaHoSoCapGCN.Text = MaHoSoCapGCNcu;
                    DataRow[] drs= dtfull.Select("MaHoSoCapGCN="+txtMaHoSoCapGCN.Text);
                    dttmp = dtfull.Clone();
                    foreach (DataRow dr in drs)
                    {
                        dttmp.ImportRow(dr);
                    }
                    ctrlGridView1.DataSource = dttmp;
                    kt = true;
                    break;
                }
            }
            if (kt == false)
            {               
               txtMaHoSoCapGCN.Text = MaHoSoCapGCNcu;
               ctrlGridView1.DataSource = null;
               ctrlGridView1.Enabled = false;
            }
        }
        private void grpTimKiem_Enter(object sender, EventArgs e)
        {

        }     
    }
}
